/*员工列表*/
$(function () {
	$("#jqGrid").jqGrid({
		url: baseURL + 'fun/emp/list',
		datatype: "json",
		colModel: [
			{ label: '员工ID', name: 'empId', index: "emp_id", width: 200, key: true},
			{ label: '员工名', name: 'empName', sortable: false,width: 250 },
			{ label: '身份证号', name: 'idCard',sortable: false, width: 100 },
			{ label: '所属部门', name: 'deptName', sortable: false, width: 150 },
			{ label: '岗位状态', name: 'status', sortable: false, width: 100 , formatter: function(value, options, row){
					if(value === 0)
						return '<span >在岗</span>'
					else if(value === 5)
						return '<span >不在岗</span>'
				}},
		],
		viewrecords: true,
		height: 400,
		rowNum: 20,
		rowList : [10,30,50],
		rownumbers: true,
		rownumWidth:25,
		autowidth:true,
		multiselect: true,
		pager: "#jqGridPager",
		jsonReader : {
			root: "page.list",
			page: "page.currPage",
			total: "page.totalPage",
			records: "page.totalCount"
		},
		prmNames : {
			page:"page",
			rows:"limit",
			order: "order"
		},
		gridComplete:function(){
			//隐藏grid底部滚动条
			//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
			$("#jqGrid").setGridParam().hideCol("empId").trigger("reloadGrid");
		},
	});
});
//部门结构树
var dept_ztree;
var dept_setting = {
	data: {
		simpleData: {
			enable: true,
			idKey: "deptId",
			pIdKey: "parentId",
			rootPId: -1
		},
		key: {
			url:"nourl"
		}
	}
};

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			empName: null
		},
		showList: true,
		title: null,
		emp: {
			remarks:null,
			status:null
		},
		pickerOptions: {
			disabledDate(time) {
				return time.getTime() > Date.now();
			}
		},
		dialogFormVisible: false,
		dialogDeptVisible: false,
		dialog: false,
		drawer: false,
		formLabelWidth: '80px',
		timer: null,
		dialog: false,
		drawer: false,
		formLabelWidth: '80px',
		timer: null,
		changeStatusEmpIds:null,
		status: [{value:'1',label:'死亡'},{value:'2',label:'离职'},{value:'3',label:'退休'},{value:'4',label:'调出'}],
	},

	methods: {
		query: function () {
			vm.reload();
		},
		handleCloseDrawer:function(done) {
			vm.dialog=false;
		},
		cancelForm:function() {
			vm.dialog = false;
		},
		handlePreview:function(file) {
			console.log(file);
		},
		handleRemove:function(file, fileList) {
			console.log(file, fileList);
		},
		fresh:function (event)
		{
			$("#jqGrid").trigger("reloadGrid");
			vm.dialogFormVisible=false;
		},
		freshDept:function (event)
		{
			$("#jqGrid").trigger("reloadGrid");
			vm.dialogDeptVisible=false;
		},
		removeEmp:function()
		{
			vm.changeStatusEmpIds =getSelectedRows();
			if(vm.changeStatusEmpIds == null){
				return ;
			}
			vm.emp={
				status:null,
				remarks:null
			};
			vm.dialog = true;
		},
		changeStatus:function() {
			if(vm.emp.status==null)
			{
				alert('减少原因不能为空');
				return;
			}
			$.ajax({
				type: "POST",
				url: baseURL + "fun/emp/changeStatus",
				contentType: "application/json",
				data: JSON.stringify({'empIds':vm.changeStatusEmpIds,'status':vm.emp.status,'remarks':vm.emp.remarks}),
				success: function(r){
					if(r.code == 0){
						vm.dialog = false;
						alert('操作成功', function(index){
							$("#jqGrid").trigger("reloadGrid");
						});

					}else{
						alert(r.msg);
					}
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
				postData:{'name': vm.q.empName,'deptId':''},
				page:page
			}).trigger("reloadGrid");
		},
		back: function (event) {
			history.go(-1);
		}
	}
});
window.addEventListener("message",function(event){
	vm.showList = true;
	var page = $("#jqGrid").jqGrid('getGridParam','page');
	$("#jqGrid").jqGrid('setGridParam',{
		postData:{'deptId':event.data,'name': ''},
		page:page
	}).trigger("reloadGrid");
}, false);